<template>
  <div class="login">
    <h3>验证码登录页面</h3>
    <!-- <el-alert v-if="isFalse" title="登陆失败" type="error" show-icon></el-alert> -->
    <el-input class="input1" v-model="account" placeholder="请输入手机"></el-input>
    <el-input class="input2" v-model="code" placeholder="请输入验证码"></el-input>
    <el-button @click="getctcode" :disabled="canUse" class="btn1" type="primary" round>获取验证码</el-button>
    <!-- <el-button @click="setCookie" class="btn3" type="success" plain>设置cookie</el-button> -->
    <el-button @click="loginbyphone" class="btn2" type="warning" disabled round>登录</el-button>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { loginbyctcodeAPI, getctcodeAPI } from "@/api/login.js";
export default {
  name: "account-code",
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      account: "",
      code: "",
      canUse: false,
      isFalse: false,
      userdata: {},
      cookie: "",
      response: "",
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    async getctcode() {
      //  const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/   //全面版
      try {
        const { data } = await getctcodeAPI(this.account);
        console.log(data, "验证码登录");
      } catch (error) {
        console.log(error);
      }
    },
    async loginbyphone() {
      if (this.account != "") {
        try {
          const data = await loginbyctcodeAPI(this.account, this.code);
          this.$store.state.isLogin = true;
          this.$store.commit("setPersonAccount", data);
          this.$store.commit("setUser", data.data.cookie);
          this.$router.push("/findmusic/discover");
        } catch (error) {
          console.log("登录失败");
          this.$alert("建议切换二维码登录", "登陆失败", {
            confirmButtonText: "确定",
          });
          return;
        }
      }
    },

    setCookie() {
      // 匹配网易云cookie
      let pattern =
        /[a-zA-Z0-9_=]*[;] [a-zA-Z0-9=-]*; [a-zA-Z0-9=, :]*; Path=[/a-zA-Z0-9]*[;]/g;
      // 获取cookie
      let userdata = this.$store.state.PersonAccount.data;
      let cookiematch = userdata.cookie;
      let array = cookiematch.match(pattern);
      array.forEach((element) => {
        document.cookie = element;
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang='less'>
.login {
  position: relative;
  h3 {
    position: absolute;
    top: 20px;
    left: 190px;
  }
  .input1 {
    position: absolute;
    width: 300px;
    left: 100px;
    top: 100px;
  }
  .input2 {
    position: absolute;
    left: 100px;
    top: 150px;
    width: 300px;
  }
  .btn1 {
    position: absolute;
    top: 250px;
    left: 100px;
  }
  .btn2 {
    position: absolute;
    top: 250px;
    right: 100px;
  }
}
</style>